<?php
/* 小黄鸡抓取程序*/
	
	print_R($_POST);exit;

	$mess = '...';
	$cookie = "__utma=119922954.1331643638.1363919714.1364451081.1364451081.1; __utmb=119922954.22.9.".time()."; __utmc=119922954; __utmz=119922954.1364451081.5.3.utmcsr=tieba.baidu.com|utmccn=(referral)|utmcmd=referral|utmcct=/p/2166507443; JSESSIONID=81648D8A5085507A052FA3DD3EB40A76; sagree=true";

	$url = "http://www.simsimi.com/func/req?msg=".$mess."&lc=zh";  
	$url = "http://pic.dev/dev/vote.html";  
	$ch = curl_init();    
    curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_GET, true); 
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
	curl_setopt($ch, CURLOPT_REFERER, "http://www.simsimi.com/talk.htm");
	$content = curl_exec($ch);
    curl_close($ch);
	//print_r($content);exit;
	preg_match('/value=\"([^\"]+)\"/',$content,$m);
	//return $m[1];
print_r($m[1]);exit;

//小I数据获取程序。
//对应 /j-tool/simsim.js
class verfiy
{
	public function m($z)
	{
		$l=7;
		$B = "";
		$C = substr(M_PI,0,$l);
		for($A=0;$A<=$l;$A++)
		{
			$D = $C{$A};
			if($D != ".")
			{
				$B = $D.$B;
			}
		}
		return $this->i($z.$B);
	}

	private function i($k)
	{
		return $this->h($this->g($this->t($k)));
	}

	private function t($A)
	{
	  $k = ((strlen($A)+8)>>6)+1;
	  for($z=0;$z<$k*16;$z++)
	  {
		$B[$z]=0;
	  }
	  for($z=0;$z<strlen($A);$z++)
	  {	  
		$B[$z>>2]|= $this->charCodeAt($A{$z})<<(24-($z&3)*8);
	  }
	  $B[$z>>2]|= 128<<(24-($z&3)*8);
	  $B[$k*16-1] = strlen($A)*8;

	  return $B;
	} 

	private function charCodeAt($t)
	{
		$code = array(
			'a'=>97,
			'b'=>98,
			'c'=>99,
			'd'=>100,
			'e'=>101,
			'f'=>102
		);
		for($i=0;$i<10;$i++)
		{
			$code[] = $i+48;
		}
		return $code[$t];
	}
	 
	private function g($N)
	{
		$L=Array();
		$K=$N;
		$J=1732584193;
		$I=-271733879;
		$H=-1732584194;
		$G=271733878;
		$F=-1009589776;
		for($C=0;$C<count($N);$C+=16)
		{

			$E=$J;
			$D=$I;
			$B=$H;
			$A=$G;
			$k=$F;
			for($z=0;$z<80;$z++)
			{
				if($z<16){
					$L[$z]=$N[$C+$z];
				}else{
						$L[$z] = $this->o($L[$z-3]^$L[$z-8]^$L[$z-14]^$L[$z-16],1);
				}
				$M = $this->u($this->u($this->o($J,5),$this->x($z,$I,$H,$G)),$this->u($this->u($F,$L[$z]),$this->l($z)));
				$F=$G;
				$G=$H;
				$H=$this->o($I,30);
				$I=$J;
				$J=$M;
			}
			$J=$this->u($J,$E);
			$I=$this->u($I,$D);
			$H=$this->u($H,$B);
			$G=$this->u($G,$A);
			$F=$this->u($F,$k);
		}
		return Array($J,$I,$H,$G,$F);
	}

	private function u($k,$B)
	{
		$A = ($k&65535)+($B&65535);
		$z = ($k>>16)+($B>>16)+($A>>16);
		return($z<<16)|($A&65535);
	}
	
	private function o($k,$z)
	{
		//return($k<<$z)|($k>>>(32-$z));//php 无符号右移，用方法代替
		return($k<<$z)|$this->moveleft($k,32-$z);
	}

	private function l($k)
	{
		return($k<20)?1518500249:(($k<40)?1859775393:(($k<60)?-1894007588:-899497514));
	}
	
	private function x($z,$k,$B,$A)
	{
		if($z<20){
			return($k&$B)|((~$k)&$A);
		}
		if($z<40){
			return $k^$B^$A;
		}
		if($z<60){
			return($k&$B)|($k&$A)|($B&$A);
		}
		return $k^$B^$A;
	}

	public function h($A)
	{
	  $z = "0123456789abcdef";
	  $B = "";
	  for($k=0;$k<count($A)*4;$k++)
	  {
		$B .= $z{($A[$k>>2]>>((3-$k%4)*8+4))&15}.$z{($A[$k>>2]>>((3-$k%4)*8))&15};
	  }
	  return $B;
    }

	public function moveleft($k,$m)
	{
		if($k > 0)
		{
			$string = DecBin($k>>$m);			
		}else{
			$string = substr(DecBin($k>>$m),$m);
		}
		return bindec($string);
	}

	public function getSig($w,$y)
	{
		return $this->i($this->m($w).$y);
	}
}

function get_cookie_xiaoi()
{
	$header=array("Content-Type: text/xml;charset=utf-8");
	$url = "http://222.73.115.170/robot/demo/wap/wap-demo.action?requestContent="; 
	$ch = curl_init();    
			curl_setopt($ch, CURLOPT_URL, $url);    
			curl_setopt($ch, CURLOPT_HEADER, 1);    
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
	$content = curl_exec($ch);    
	curl_close($ch);    
	list($header, $body) = explode("\r\n\r\n", $content);    
	preg_match_all("/set\-cookie:([^\r\n]*)/i", $header, $matches);   
	foreach($matches[1] as $val)
	{
		$temp = explode(';',str_replace(array(';Path=/robot',';Path=/'),'',$val));
		$getcookie[] = $temp[0];
	}
	$sessionid = trim($getcookie[0]);
	$nonce = trim(str_replace('nonce=','',end($getcookie)));
	$cnonce = rand(100000,1000000);
	$cvisits = time();

	$verfiy = new verfiy();
	$sig = $verfiy->getSig($nonce,$cnonce);
	
	$cookies = 'cnonce='.$cnonce.'; sig='.$sig.'; cvisits=0%2C'.$cvisits.'; '.$sessionid.'; '.end($getcookie);
	return $cookies;
}

function get_xiaoi($mess) 
{
	$cookie = get_cookie_xiaoi();	
	$url = "http://222.73.115.170/robot/demo/wap/wap-demo.action?requestContent=".$mess;  
	$ch = curl_init();    
    curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_GET, true); 
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1; rv:20.0) Gecko/20100101 Firefox/20.0");
	$content = curl_exec($ch);   
    curl_close($ch);
	$match_name = '/<p[^>]*class=\"wap_cn2\">.*?<\/span>(.*?)<\/p>/si';
	preg_match($match_name,$content,$m);
	return $m[1];
}

$mess = $_GET['msg']?$_GET['msg']:'哈哈';
$str = get_xiaoi($mess);

?> 
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>小I机器人对话框</title>
<meta content="" name="description">
<script type="text/javascript" src="/j-tool/jquery.js"></script>
<script>
	function ask()
	{
		var val = $('#msg').val();
		window.location = "http://pic.dev/dev/simsim.php?msg="+val;
	}
</script>
<style>
	div{height:30px;line-height:30px;font-size:12px;}
</style>
<body>
	<div><input type="text" name="msg" id="msg" value="哈哈"/><button onclick="ask();">提问</button></div>
	<div><font>回答：</font><font color="green"><?php echo $str;?></font></div>
</body>
</html>